package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.SearchResultPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.SearchResultView
import com.weetop.cfw.bean.*
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.constants.Constants
import com.weetop.cfw.home_page.activity.SearchResultActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class SearchResultPresenterImp(var searchResultView: SearchResultView? = null): SearchResultPresenter {
    /**
     * 获取分类的数据
     */
    override fun getKindData(searchResultActivity: SearchResultActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().kind(),searchResultActivity,object:
            RxJavaCallBack<KindBean>(){
            override fun onSuccess(t: KindBean?) {
                searchResultView?.kindDataGetSuccess(t)
            }
        })
    }

    /**
     * 获取面积的数据
     */
    override fun getAreaData(searchResultActivity: SearchResultActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().generalCodeTable(1100),searchResultActivity,object:RxJavaCallBack<GeneralCodeTableBean>(){
            override fun onSuccess(t: GeneralCodeTableBean?) {
                searchResultView?.areaDataGetSuccess(t)
            }
        })
    }

    /**
     * 获取区域的数据
     */
    override fun getQuYuData(searchResultActivity: SearchResultActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().quYu(Constants.cityId),searchResultActivity,
            object : RxJavaCallBack<SiteBean>() {
                override fun onSuccess(t: SiteBean?) {
                    searchResultView?.quYuGetSuccess(t)
                }
            })
    }

    /**
     * 获取价格过滤的数据
     */
    override fun getPriceData(searchResultActivity: SearchResultActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().generalCodeTable(2300),searchResultActivity,object:RxJavaCallBack<GeneralCodeTableBean>(){
            override fun onSuccess(t: GeneralCodeTableBean?) {
                searchResultView?.priceFilterDataGetSuccess(t)
            }
        })
    }

    /**
     * 获取行业过滤的数据
     */
    override fun getIndustryData(searchResultActivity: SearchResultActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().generalCodeTable(1700),searchResultActivity,object:RxJavaCallBack<GeneralCodeTableBean>(){
            override fun onSuccess(t: GeneralCodeTableBean?) {
                searchResultView?.industryFilterDataGetSuccess(t)
            }
        })
    }

    /**
     * 获取来源过滤的数据
     */
    override fun getSourceData(searchResultActivity: SearchResultActivity) {
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().generalCodeTable(1800),searchResultActivity,object:RxJavaCallBack<GeneralCodeTableBean>(){
            override fun onSuccess(t: GeneralCodeTableBean?) {
                searchResultView?.sourceFilterDataGetSuccess(t)
            }
        })
    }

    /**
     * 获取搜索结果
     */
    override fun getSearchResultData(searchResultActivity: SearchResultActivity,queryMap: Map<String, String>) {
        searchResultActivity.showLoadingDialog(searchResultActivity,"正在获取搜索结果...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getSearchResultData(queryMap = queryMap),searchResultActivity,object:RxJavaCallBack<WorkshopWarehouseSearchResultBean>(){
            override fun onSuccess(t: WorkshopWarehouseSearchResultBean?) {
                searchResultActivity.dismissLoadingDialog()
                searchResultView?.searchResultGetSuccess(t)
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                searchResultActivity.dismissLoadingDialog()
                searchResultView?.searchResultGetFailed()
            }

            override fun onRequestError(t: WorkshopWarehouseSearchResultBean?) {
                super.onRequestError(t)
                searchResultActivity.dismissLoadingDialog()
                searchResultView?.searchResultGetFailed()
            }

        })
    }

    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        searchResultView = baseView as? SearchResultView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        searchResultView = null
    }
}